package com.sx.micowell.framework.model

import android.annotation.SuppressLint
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.example.mvvm.base.data.MutableLiveListData
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.rx.RxBus
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.SPUtils
import com.example.mvvm.until.ToastUtils
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.DictType
import com.miloyu.modelnet.response.PlanTypeListBean
import com.sx.micowell.framework.app.AppConstant
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.tools.EventInfo
import io.reactivex.android.schedulers.AndroidSchedulers

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/7/31 14:24
 */
class PhysicalInfoModel :BaseViewModel() {

    override fun registerRxBus() {
        super.registerRxBus()
        addSubscribe(
            RxBus.getDefault().toObservable(EventInfo::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { event ->
                    if (event.type == AppConstant.EVENT_PERFECT_INFO_SUCCESS) {
                        // 注册成功、修改密码成功
                        context.finishEvent.call()
                    }
                }
        )
    }

    val initWeightData :MutableLiveData<String> =MutableLiveData();                 //初始化体重
    val targetWeightData :MutableLiveData<String> = MutableLiveData();               //目标体重

    var trainTypeEvent : MutableLiveListData<DictType> = MutableLiveListData()

    /**
     * 查询训练类型
     */
    @SuppressLint("CheckResult")
    fun queryTrainType() {
        addSubscribe(
            HttpClient.instance.apiService
                .queryPlanType()
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<PlanTypeListBean>() {

                    override fun success(data: PlanTypeListBean?, msg: String?) {
                        LogUtils.d("queryTrainType ${data?.dictType?.get(0)?.weeks!![0].dictLabel}")
                        trainTypeEvent.addData(data.dictType)
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {
                        LogUtils.d("queryTrainType $errorMsg")
                    }

                })
        )

    }

    fun perfectInfo( birthday: String, sex: String, height: String,
                     weight: String, targetWeight: String, target: String?) {
        addSubscribe(
            HttpClient.instance.apiService
                .perfectInfo(birthday, sex, height, weight, targetWeight, target)
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<Void?>() {

                    override fun success(data: Void?, msg: String?) {
                        ToastUtils.showShort("保存成功")
                        //SPUtils.getInstance().put(SPKey.SP_AUTH, true)
                        RxBus.getDefault().post(EventInfo(AppConstant.EVENT_PERFECT_INFO_SUCCESS))
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {

                    }

                })
        )

    }
}